package yxy.game.pm2.active.command.prerogative;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.chargeshop;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_特权商城;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActPrerogativeBuyRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;

@MessageType(proto = Command.ActPrerogativeBuy, classtype = ActiveMessage.ActPrerogativeBuyRequest.class)
public class CMD_Active_特权商品_Buy extends Command<ActPrerogativeBuyRequest> {

	@Override
	public void execute(MessageSource messageSource, ActPrerogativeBuyRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActPrerogativeBuyResponse.Builder response = ActiveMessage.ActPrerogativeBuyResponse.newBuilder();
		int csid = request.getCsid();
		
		ActiveModule_特权商城 am = ActiveModule_特权商城.get(pcode);
		if(am == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		chargeshop cs = DataCenter.getData(csid, chargeshop.class);
		if(cs == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		int purchasetype = cs.getPurchasetype();
		
		//付费购买
		if(purchasetype == 1) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		//元宝购买
		if(purchasetype == 2) {
			int state = am.canbuy(csid);
			if(state == DrawStatus.hadget.getStatus()) {
				send(messageSource, response, Result.ActHadGet);
				return;
			}
			int purchasever = cs.getPurchasever();
			Currency costcur = Currency.create(CurrencyType.CT_元宝, -1, purchasever);
 			
			try {
				new CurrencyHandle().cost(pcode, CCReason.活动_特权商品, costcur);
			} catch (CurrencyNotEnoughException e) {
				send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
				return;
			}
			
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(cs.getRewardtype(), cs.getRewardid(), cs.getRewardnum());
			new CurrencyHandle().addCurrency(pcode, CCReason.活动_特权商品, curs);
			am.addprerogative(csid, cs);
			
//			JSONArray arr = new JSONArray();
			for(int i = 0; i < curs.size(); i++) {
				PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
				
				Currency c = curs.get(i);
				item.setType(c.getType().getNumber());
				item.setDataId(c.getDataId());
				item.setNumber(c.getNumber());
				response.addDisplay(item.build());
				response.addUpdate(item.build());
			}

			send(messageSource, response, Result.Success);
			return;
		}
		
		send(messageSource, response, Result.DataError);
		return;
	}

	private void send(MessageSource messageSource, ActiveMessage.ActPrerogativeBuyResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
